package com.blue.controller.job;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.annotation.ReplaceParam;
import com.blue.common.entity.ResponseResult;
import com.blue.common.utils.ConvertUtils;
import com.blue.common.utils.QueryWrapperUtils;
import com.blue.dto.job.JobLogsDTO;
import com.blue.po.job.JobLogsPO;
import com.blue.service.job.IJobLogsService;
import com.github.yulichang.query.MPJQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 任务调度日志表
 *
 * @author cnj
 * @email 15696471158@163.com
 * @date 2023-10-04
 */
@RestController
@RequestMapping("job/jobLogs")
public class JobLogsController {
    @Autowired
    private IJobLogsService jobLogsService;

    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    @ReplaceParam("className,remark")
    public ResponseResult<?> list(JobLogsDTO requestDTO,
                                  @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        String startTime = requestDTO.getStartTime();
        String endTime = requestDTO.getEndTime();
        JobLogsPO jobLogsPO = ConvertUtils.convert(JobLogsPO.class, requestDTO);
        MPJQueryWrapper<JobLogsPO> queryWrapper = QueryWrapperUtils.initQueryWrapper(jobLogsPO);
        queryWrapper.ge(StringUtils.isNotBlank(startTime), "t.create_time", startTime);
        queryWrapper.le(StringUtils.isNotBlank(endTime), "t.create_time", endTime);
        Page<JobLogsPO> page = new Page<>(pageNum, pageSize);
        Page<JobLogsPO> pageResult = jobLogsService.page(page, queryWrapper);
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseResult<?> add(@RequestBody JobLogsDTO requestDTO) {
        JobLogsPO jobLogsPO = ConvertUtils.convert(JobLogsPO.class, requestDTO);
        jobLogsService.save(jobLogsPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/edit")
    @Operation(summary = "修改")
    public ResponseResult<?> edit(@RequestBody JobLogsDTO requestDTO) {
        JobLogsPO jobLogsPO = ConvertUtils.convert(JobLogsPO.class, requestDTO);
        jobLogsService.updateById(jobLogsPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    public ResponseResult<?> edit(@RequestBody List<String> ids) {
        jobLogsService.removeBatchByIds(ids);
        return ResponseResult.okResult();
    }

    @PostMapping("/queryById")
    @Operation(summary = "通过ID查询")
    public ResponseResult<?> queryById(@RequestBody JobLogsDTO requestDTO) {
        JobLogsPO jobLogsPO = jobLogsService.getById(requestDTO.getId());
        return ResponseResult.okResult(jobLogsPO);
    }
}
